package config

import (
	"context"
	"fmt"

	"github.com/sirupsen/logrus"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

var MgoClient = initMongoDB()

func initMongoDB() *mongo.Client {
	config := Configs.MongoDB
	// 设置客户端连接配置
	clientOptions := options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%d", config.Host, config.Port))

	// 连接到MongoDB
	client, err := mongo.Connect(context.TODO(), clientOptions)
	if err != nil {
		logrus.Error(err)
		return nil
	}
	return client
}
